package com.wc.alorithm_blue_bridge._图论.团建;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/13 11:01
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3444437&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * 用 map 映射 值 -> id O(n)
     * 用 set 保存 是否相等, 可以快速比较是否存在 O(n)
     * 用 dfs 保证前缀相等, 遍历即可 O(n)
     * 用 图论 保存边, 权值为 u -> v,  w[v]
     * 整体O(n)
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010, M = N << 2;
    static int[] h1 = new int[N], h2 = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] w1 = new int[N], w2 = new int[N];
    static HashMap<Integer, Integer> map = new HashMap<>();
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            w1[i] = sc.nextInt();
            map.put(w1[i], i);
        }
        for (int i = 1; i <= m; i++) w2[i] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(h1, a, b, w1[b]);
            add(h1, b, a, w1[a]);
        }

        for (int i = 1; i < m; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(h2, a, b, w2[b]);
            add(h2, b, a, w2[a]);
        }
        int res;
        if (w1[1] != w2[1]) res = 0;
        else res = dfs(1, -1, 1, -1);
        out.println(res);
        out.flush();
    }

    static void add(int[] h, int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static int dfs(int u1, int f1, int u2, int f2) {
        int res = 0;
        Set<Integer> set = new HashSet<>();
        // 保存可以到达的位置
        for (int i = h1[u1]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == f1) continue;
            set.add(w[i]);
        }

        // 检查下一个节点是否相等
        for (int i = h2[u1]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == f2) continue;
            if (set.contains(w[i]))
                res = Math.max(res, dfs(map.get(w[i]), u1, j, u2));
        }

        return res + 1;
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

